import org.apache.spark.api.java.*;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.function.Function;

public class TestSparkJava
{
    public static void main(String[] args)
    {
        String logFile = "/home/appleyuchi/IdeaProjects/SpringBoot2EnterPrise/第4章-SpringBoot的数据访问/testSpark/ab.txt";

        SparkConf conf = new SparkConf().setMaster("spark://Desktop:7077")

                .setJars(new String[]{"/home/appleyuchi/IdeaProjects/SpringBoot2EnterPrise/第4章-SpringBoot的数据访问/testSpark/target/testSpark-1.0-SNAPSHOT.jar"})
                .setAppName("TestSpark");
        
        JavaSparkContext sc = new JavaSparkContext(conf);

        JavaRDD<String> logData = sc.textFile(logFile).cache();

        long numAs = logData.filter(new Function<String, Boolean>()
        {
            public Boolean call(String s) { return s.contains("0");
        }
        }).count();

        long numBs = logData.filter(new Function<String, Boolean>()
        {
            public Boolean call(String s) { return s.contains("1");
        }
        }).count();

        System.out.println("Lines with 0: " + numAs + ", lines with 1: " + numBs);

        sc.stop();
    }
}